package meijuli.com.join51.business.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import meijuli.com.join51.business.R;
import com.jhx.common.http.ApiManager;
import com.jhx.common.http.HttpRequest;
import com.jhx.common.http.HttpResponse;
import com.jhx.common.http.ResponseCallback;
import meijuli.com.join51.business.location.LocationActivity;
import com.jhx.common.model.EventBean;
import meijuli.com.join51.business.model.LocationDetails;
import com.jhx.common.model.User;
import com.jhx.common.utils.ToastUtil;

import meijuli.com.join51.business.service.ApiService;
import com.jhx.common.utils.UserManager;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import retrofit2.Call;

/**
 * 请描述使用该类使用方法！！！
 *
 * @author wenqin 2017-04-25 16:26
 */

public class AddressActivity extends BaseActivity {
    /**
     * 打开地图
     */
    private static final int REQUEST_CODE_MAP_LOCATION = 130;
    TextView mToolbarTitle;
    TextView mTextMenu;
    Toolbar mToolbar;
    TextView mTitle;
    TextView mTextAddress;
    ImageView mAddressIcon;
    RelativeLayout mRlAddress;
    Button mSubmitBtn;
    EditText mDetailAddress;
    private Call<HttpResponse<User>> mCall;
    private double mLongitude;
    private double mLatitude;
    private long mDmId;

    public static void start(Activity activity) {
        Intent intent = new Intent(activity, AddressActivity.class);
        activity.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_address);
        mToolbarTitle = findViewById(R.id.toolbar_title);
        mTextMenu = findViewById(R.id.text_menu);
        mToolbar = findViewById(R.id.toolbar);
        mTitle = findViewById(R.id.title);
        mTextAddress = findViewById(R.id.text_address);
        mAddressIcon = findViewById(R.id.address_icon);
        mRlAddress = findViewById(R.id.rl_address);
        mSubmitBtn = findViewById(R.id.submit_btn);
        mDetailAddress = findViewById(R.id.detail_address);
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        initToolBar();

        User user = UserManager.get().getUser();
        if (UserManager.get().checkUserNull(this)) {
            return;
        }
        mDmId = user.getDmId();
        mLatitude = user.getLatitude();
        mLongitude = user.getLongitude();
        mTextAddress.setText(user.getAddress());
        mDetailAddress.setText(user.getShopAdd());
        mRlAddress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //启动定位
                Intent intent = new Intent(AddressActivity.this, LocationActivity.class);
                startActivityForResult(intent, REQUEST_CODE_MAP_LOCATION);
            }
        });

        mSubmitBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                submit();
            }
        });

    }

    /**
     * 提交新地址
     */
    private void submit() {
        if (mLatitude > 0 && mLongitude > 0 && !TextUtils.isEmpty(mTextAddress.getText())
                && !TextUtils.isEmpty(mDetailAddress.getText())) {
            if ("null".equals(mDetailAddress.getText().toString())) {
                ToastUtil.getInstance().showToast(this, "非法字符");
                return;
            }
            showLoadingDialog();
            mCall = ApiManager.get().getApiService(ApiService.class).changeAddress(mLongitude,
                    mLatitude, mTextAddress.getText().toString(),
                    mDmId, mDetailAddress.getText().toString());
            HttpRequest.getRequest().autoRecycle(this).start(mCall, new ResponseCallback<User>() {

                @Override
                public void onSuccess(String url, User data) {
                    dismissDialog();
                    ToastUtil.getInstance().showToast(AddressActivity.this, "更改定位成功");
                    UserManager.get()
                            .setLatitude(data.getLatitude())
                            .setLongitude(data.getLongitude())
                            .setAddress(data.getAddress())
                            .setShopAddress(data.getShopAdd());
                    finish();
                }

                @Override
                public void onError(String url, int errorCode, String errorMsg) {
                    super.onError(url, errorCode, errorMsg);
                    dismissDialog();
                }
            });
        } else {
            ToastUtil.getInstance().showToast(this, "获取地址失败或者详细地址不能为空");
        }
    }

    private void initToolBar() {
        mToolbar.setTitle("");
        mToolbarTitle.setText("修改商铺地址");
        mToolbar.setNavigationIcon(R.mipmap.icon_back_white);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }

    @Subscribe
    public void onEvent(EventBean event) {
        if (event == null) {
            return;
        }
        if (event.getStatus() == EventBean.STATUS_UPDATE_LOCATION) {
            LocationDetails locationDetails = (LocationDetails) event.getEvent();
            if (locationDetails != null && !TextUtils.isEmpty(locationDetails.getCity())) {
                mTextAddress.setText(locationDetails.getProvince() + locationDetails.getCity() +
                        locationDetails.getDistrict());
                mDetailAddress.setText(locationDetails.getStreet());
                mLatitude = locationDetails.getLatitude();
                mLongitude = locationDetails.getLongitude();
            }
        }
    }
}
